package com.hjn.springboot.springbootbacklog.filter;

import java.io.File;

/**
 * @author: huaijn
 * @create: 2022/5/2 7:50
 * @description: todo
 **/
public class FileFilter {
    /*文件路径*/
    private final static String FILE_PATH = "D:\\.m2\\repository";
    /*需要删除的文件名*/
    private final static String DEL_FILE_NAME = "_remote.repositories";

    public static void main(String[] args) {
        File file = new File(FILE_PATH);
        init(file);
    }

    /*加载文件*/
    private static void init(File file) {

        if (!file.exists()) {
            System.out.println("文件不存在");
            return;
        }
        if (file.isFile()) {
            doFile(file);
        } else if (file.isDirectory()) {
            String name = file.getName();
            String absolutePath = file.getAbsolutePath();
            System.out.println("文件路径是：" + absolutePath + ";文件名是：" + name);
//        循环文件路径，查看是文件还是目录
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                init(files[i]);
            }
        }
    }

    /*单春文件*/
    public static void doFile(File file) {
        String name = file.getName();
        String absolutePath = file.getAbsolutePath();
        boolean isFile = file.isFile();
        if (name.equals(DEL_FILE_NAME)) {
            file.delete();
            System.out.println("被删除的文件路径是：" + absolutePath + ";文件名是：" + name);
        }
    }
}
